package com.gyg.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.actuate.health.CompositeHealthContributor;
import org.springframework.boot.actuate.health.HealthContributor;
import org.springframework.boot.actuate.health.NamedContributor;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.*;

/**
 * 支持监控多个线程池的状态
 */
@Component
public class MyThreadPoolCompositeHealthConfig implements CompositeHealthContributor {

    private Map<String, HealthContributor> map = new HashMap<>();
    

    @PostConstruct
    public void init() {
        ThreadPoolConfig threadPoolConfig = new ThreadPoolConfig();
        threadPoolConfig.createThreadPoolExecutor();
        MyThreadPollHealthConfig healthConfig = new MyThreadPollHealthConfig(threadPoolConfig.getThreadPool());
        map.put("MyThreadTest", healthConfig);
        map.put("MyThreadTest2", healthConfig);
    }

    @Override
    public HealthContributor getContributor(String name) {
        return map.get(name);
    }

    @Override
    public Iterator<NamedContributor<HealthContributor>> iterator() {
        List<NamedContributor<HealthContributor>> contributors = new ArrayList<>();
        map.forEach((name, c) -> {
            contributors.add(NamedContributor.of(name, c));
        });
        return contributors.iterator();
    }
}
